package com.happy.ibidding.controller;

import com.happy.ibidding.bean.Bid;
import com.happy.ibidding.bean.Comment;
import com.happy.ibidding.service.AskService;
import com.happy.ibidding.service.BidService;
import com.happy.ibidding.service.CommentService;
import com.happy.ibidding.service.UserService;
import com.happy.ibidding.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("api/comment")
public class CommentController {

    final private UserService userService;

    final private CommentService commentService;

    @Autowired
    public CommentController(UserService userService, CommentService commentService) {
        this.userService = userService;
        this.commentService = commentService;
    }


    @PostMapping
    public Comment comment(@RequestBody Comment comment) {
        comment.setPublishTime(System.currentTimeMillis());
        return this.commentService.save(comment);
    }

    @GetMapping("/all")
    public List<Comment> viewAll(@RequestParam(required = false) String askId, @RequestParam(required = false) String userId) {
        List<Comment> commentList = this.commentService.viewAll(askId, userId);

        commentList.parallelStream().forEach(each -> {
            each.setUser(this.userService.findUserByUserId(each.getUserId()));
        });
        return commentList;
    }

    @GetMapping("/delete/{mongodbId}")
    public void delete(@PathVariable String mongodbId) {

        this.commentService.delete(mongodbId);
    }



}
